package expression;

import common.MCore;

import constraint.MPredicate;

public abstract class MBoundedIntVar extends MIntVar {
	protected int lowerBound;
	protected int upperBound;
	
	public MBoundedIntVar(MCore c, String n) {
		super(c, n);
		lowerBound = MIntVar.MIN_VALUE;
		upperBound = MIntVar.MAX_VALUE;
	}
	
	public boolean isEmpty() {
		return lowerBound > upperBound;
	}

	public int getLowerBound() {
		return lowerBound;
	}
	
	public boolean setLowerBound(int i) {
		if (lowerBound < i) {
			lowerBound = i;
			return true;
		}
		return false;
	}
	
	public int getUpperBound() {
		return upperBound;
	}
	
	public boolean setUpperBound(int i) {
		if (i < upperBound) {
			upperBound = i;
			return true;
		}
		return false;
	}
	
	public void reset() {
		if ((upperBound - lowerBound) < MAX_ENUM) {
			variable = getCore().getProblem().makeEnumIntVar(name, lowerBound, upperBound);
		}
		else {
			variable = getCore().getProblem().makeBoundIntVar(name, lowerBound, upperBound);
		}
	}
	
	public String toString() {
		String s = name + " \t";
		if (lowerBound == upperBound)
			s += " [" + lowerBound + "]";
		else
			s += " [" + lowerBound + " ; " + upperBound + "]";
		s += " \t{ ";
		for (MIntVar v: getVarDepencies()) {
			s += v.getName() + " ";
		}
		s += "} \t{ ";
		for (MPredicate p: getPredDepencies()) {
			s += p.getName() + " ";
		}
		s += "}";
		
		return s;
	}
	
	public MIntExpr eval() {
		if (lowerBound == upperBound) {
			return new MIntCst(getCore(), lowerBound);
		}
		else {
			return this;
		}
	}
}
